package com.cxhc.system.service.app.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cxhc.common.core.domain.entity.SysUser;
import com.cxhc.common.utils.StringUtils;
import com.cxhc.system.domain.app.CmAreaInfo;
import com.cxhc.system.domain.app.RpRevHandling;
import com.cxhc.system.domain.app.SysEnterprise;
import com.cxhc.system.mapper.SysUserMapper;
import com.cxhc.system.mapper.app.CmAreaInfoMapper;
import com.cxhc.system.mapper.app.RpRevHandlingMapper;
import com.cxhc.system.mapper.app.SysUserEnterpriseMapper;
import com.cxhc.system.mapper.cockpit.SysEntMapper;
import com.cxhc.system.service.app.ICmAreaInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * 楼宇信息Service业务层处理
 * 
 * @author Dhq
 * @date 2024-08-14
 */
@Service
public class CmAreaInfoServiceImpl implements ICmAreaInfoService
{
    @Autowired
    private CmAreaInfoMapper cmAreaInfoMapper;

    @Autowired
    private SysUserEnterpriseMapper sysUserEnterpriseMapper;

    @Autowired
    private SysEntMapper sysEntMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RpRevHandlingMapper rpRevHandlingMapper;

    /**
     * 查询楼宇信息
     * 
     * @param ID 楼宇信息主键
     * @return 楼宇信息
     */
    @Override
    public CmAreaInfo selectCmAreaInfoByID(Long ID)
    {
        return cmAreaInfoMapper.selectCmAreaInfoByID(ID);
    }

    /**
     * 查询楼宇信息列表
     * 
     * @param cmAreaInfo 楼宇信息
     * @return 楼宇信息
     */
    @Override
    public List<CmAreaInfo> selectCmAreaInfoList(CmAreaInfo cmAreaInfo)
    {
        List<CmAreaInfo> cmAreaInfos = cmAreaInfoMapper.selectCmAreaInfoList(cmAreaInfo);
        for (CmAreaInfo areaInfo : cmAreaInfos) {
            String contactTel = areaInfo.getContactTel();
            String estateUnitTel = areaInfo.getEstateUnitTel();
            if (!StringUtils.isEmpty(contactTel) && contactTel.length() == 11){
                areaInfo.setContactTel(contactTel.substring(0, 3) + "****" + contactTel.substring(7));
            }
            if (!StringUtils.isEmpty(estateUnitTel) && estateUnitTel.length() == 11){
                areaInfo.setEstateUnitTel(estateUnitTel.substring(0, 3) + "****" + estateUnitTel.substring(7));
            }

        }
        return cmAreaInfos;
    }

    @Override
    public List<CmAreaInfo> getCmAreaInfoStatisticsList(CmAreaInfo cmAreaInfo) {
        List<CmAreaInfo> cmAreaInfos = cmAreaInfoMapper.selectCmAreaInfoList(cmAreaInfo);

        Map<String, Map<String, Object>> entMaps = sysEntMapper.countEntByFloors(cmAreaInfos);
        Map<String, Map<String, Object>> userMaps = sysEntMapper.countUserByFloors(cmAreaInfos);
        Map<String, Map<String, Object>> revMaps = rpRevHandlingMapper.countRevByFloors(cmAreaInfos);

        for (CmAreaInfo areaInfo : cmAreaInfos) {
            Map<String, Object> entMap = entMaps.get(areaInfo.getAreaID());
            if (!CollectionUtils.isEmpty(entMap)){
                Object entCount = entMap.get("entCount");
                if (ObjectUtils.isEmpty(entCount)){
                    areaInfo.setCompanyTotal((Long) entCount);
                }
            }

            Map<String, Object> userMap = userMaps.get(areaInfo.getAreaID());
            if (!CollectionUtils.isEmpty(entMap)){
                Object userCount = userMap.get("userCount");
                if (ObjectUtils.isEmpty(userCount)){
                    areaInfo.setEmpTotal((Long) userCount);
                }
            }

            Map<String, Object> revMap = revMaps.get(areaInfo.getAreaID());
            if (!CollectionUtils.isEmpty(revMap)){
                Object revCount = revMap.get("revCount");
                if (ObjectUtils.isEmpty(revCount)){
                    areaInfo.setCompanyTotal((Long) revCount);
                }
            }
         }
        return cmAreaInfos;
    }

    @Override
    public Map<String, String> statisticsCount() {
        Map<String,String> res = new HashMap<>();
        String areaCount = cmAreaInfoMapper.selectCount(new CmAreaInfo());
        String entCount = sysEntMapper.selectCount(new SysEnterprise());
        String userCount = sysUserMapper.selectCount(new SysUser());
        String revCount = rpRevHandlingMapper.selectCount(new RpRevHandling());
        res.put("areaCount",areaCount);
        res.put("entCount",entCount);
        res.put("userCount",userCount);
        res.put("revCount",revCount);
        return res;
    }

    @Override
    public List<Map<String, Object>> entListByAreaId(String areaId) {
        List<Map<String, Object>> maps = sysEntMapper.selectEntListByArea(areaId);
        if (!CollectionUtils.isEmpty(maps)){
            Map<String, Map<String, Object>> revMaps = rpRevHandlingMapper.countRevByEnts(maps);
            for (Map<String, Object> map : maps) {
                Map<String, Object> entMap = revMaps.get(String.valueOf(map.get("entID")));
                if (!CollectionUtils.isEmpty(entMap)){
                    Object revCount = entMap.get("revCount");
                    if (!ObjectUtils.isEmpty(revCount)){
                        map.put("revCount",revCount);
                    }else {
                        map.put("revCount",0);
                    }
                }else {
                    map.put("revCount",0);
                }
            }
        }
        return maps;
    }

    @Override
    public List<Map<String, String>> selectCmAreaInfoListForCockpit() {
        return cmAreaInfoMapper.selectCmAreaInfoListForCockpit();
    }

    /**
     * 新增楼宇信息
     * 
     * @param cmAreaInfo 楼宇信息
     * @return 结果
     */
    @Override
    public int insertCmAreaInfo(CmAreaInfo cmAreaInfo)
    {
        return cmAreaInfoMapper.insertCmAreaInfo(cmAreaInfo);
    }

    /**
     * 修改楼宇信息
     * 
     * @param cmAreaInfo 楼宇信息
     * @return 结果
     */
    @Override
    public int updateCmAreaInfo(CmAreaInfo cmAreaInfo)
    {
        return cmAreaInfoMapper.updateCmAreaInfo(cmAreaInfo);
    }

    /**
     * 批量删除楼宇信息
     * 
     * @param IDs 需要删除的楼宇信息主键
     * @return 结果
     */
    @Override
    public int deleteCmAreaInfoByIDs(Long[] IDs)
    {
        return cmAreaInfoMapper.deleteCmAreaInfoByIDs(IDs);
    }

    /**
     * 删除楼宇信息信息
     * 
     * @param ID 楼宇信息主键
     * @return 结果
     */
    @Override
    public int deleteCmAreaInfoByID(Long ID)
    {
        return cmAreaInfoMapper.deleteCmAreaInfoByID(ID);
    }

    @Override
    public Map<String, String> queryEntByTyp(String cmAreaId) {

        BigDecimal sum = new BigDecimal(sysUserEnterpriseMapper.countEnt(cmAreaId));
        Map<String,String> mapRes = new HashMap<>();
        mapRes.put("sum",sum.toString());
        List<Map<String, Object>> maps = sysUserEnterpriseMapper.countEntByType(cmAreaId);
        for (Map<String, Object> map : maps) {
            BigDecimal typeCount = new BigDecimal(String.valueOf(map.get("typeCount")));
            String industry = (String)map.get("industry");
            if (StringUtils.isEmpty(industry)){
                industry = "其他";
            }
            if (BigDecimal.ZERO.equals(typeCount)){
                mapRes.put(industry, "0");
            }else {
                mapRes.put(industry,typeCount.toString());
            }
        }
        return mapRes;
    }

    @Override
    public Map<String, Map<String, String>> queryUserCountForArea(String cmAreaId) {

        BigDecimal sum = new BigDecimal(sysUserEnterpriseMapper.countUser(cmAreaId));
        Map<String,String> mapRes = new HashMap<>();
        mapRes.put("sum",sum.toString());
        Map<String, Map<String, String>> stringMapMap = sysUserEnterpriseMapper.queryUserCountForArea(cmAreaId);
        stringMapMap.put("sum",mapRes);

        return stringMapMap;
    }
}
